package com.zygk.library.util;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;
import cn.carbs.android.avatarimageview.library.AvatarImageView;
import com.bumptech.glide.Glide;
import com.zygk.library.R;
import com.zygk.library.view.GlideCircleBorderTransform;
import com.zygk.library.view.GlideCircleTransform;
import com.zygk.library.view.GlideRoundTransform;

/**
 * Glide图片公共类
 * created by Djy
 * 2017/7/20 0020 下午 4:17
 */
public class ImageManager {
  // 好享服用户端测试
  //public static String URL = "http://hellodj.eicp.net:13872/wk";
  // 好享服用户端正式
  public static String URL = "http://syclhxf.zhyinfo.cn/hxfweb";

  public static final String ANDROID_RESOURCE = "android.resource://";
  public static final String FOREWARD_SLASH = "/";
  private Context mContext;
  private static ImageManager instance;


  public ImageManager(Context context) {
    this.mContext = context;
  }

  private String getUrl(String url){
    String res = url;
    if(!StringUtils.isBlank(url)
        //&& url.startsWith("/File/Images")
        && !url.startsWith("http")
        ){
      res = URL + url;
    }
    return res;
  }

  public void clearDiskImageCash() {
    Glide.get(mContext).clearDiskCache();
  }

  // 将资源ID转为Uri
  public Uri resourceIdToUri(int resourceId) {
    return Uri.parse(ANDROID_RESOURCE + mContext.getPackageName() + FOREWARD_SLASH + resourceId);
  }

  // 加载网络图片
  public void loadUrlImage(String url, ImageView imageView) {
    url = getUrl(url);
    Glide.with(mContext)
        .load(url)
        .placeholder(R.mipmap.default_image)
        .error(R.mipmap.default_image)
        .crossFade()
        .fitCenter()
        .into(imageView);
  }

  // 加载网络图片
  public void loadUrlImageWithDefaultImg(String url, ImageView imageView, int defaultImg) {
    url = getUrl(url);
    Glide.with(mContext)
        .load(url)
        .placeholder(defaultImg)
        .error(defaultImg)
        .crossFade()
        .fitCenter()
        .into(imageView);
  }

  // 加载drawable图片
  public void loadResImage(int resId, ImageView imageView) {
    Glide.with(mContext)
        .load(resourceIdToUri(resId))
        .placeholder(R.mipmap.default_image)
        .error(R.mipmap.default_image)
        .crossFade()
        .into(imageView);
  }

  // 加载本地图片
  public void loadLocalImage(String path, ImageView imageView) {
    Glide.with(mContext)
        .load("file://" + path)
        .placeholder(R.mipmap.default_image)
        .error(R.mipmap.default_image)
        .crossFade()
        .into(imageView);
  }

  // 加载网络圆型图片
  public void loadCircleImage(String url, ImageView imageView) {
    url = getUrl(url);
    Glide.with(mContext)
        .load(url)
        .placeholder(R.mipmap.default_image)
        .error(R.mipmap.default_image)
        .crossFade()
        .transform(new GlideCircleTransform(mContext))
        .into(imageView);
  }

  // 加载网络圆型图片-带边框
  public void loadCircleHasBorderImage(String url, ImageView imageView, int bordercolor,
      int borderwidth) {
    url = getUrl(url);
    Glide.with(mContext)
        .load(url)
        .placeholder(R.mipmap.default_image)
        .error(R.mipmap.default_image)
        .crossFade()
        .transform(new GlideCircleBorderTransform(mContext, bordercolor, borderwidth))
        .into(imageView);
  }

  // 加载drawable圆型图片
  public void loadCircleResImage(int resId, ImageView imageView) {
    Glide.with(mContext)
        .load(resourceIdToUri(resId))
        .placeholder(R.mipmap.default_image)
        .error(R.mipmap.default_image)
        .crossFade()
        .transform(new GlideCircleTransform(mContext))
        .into(imageView);
  }

  // 加载本地圆型图片
  public void loadCircleLocalImage(String path, ImageView imageView) {
    Glide.with(mContext)
        .load("file://" + path)
        .placeholder(R.mipmap.default_image)
        .error(R.mipmap.default_image)
        .crossFade()
        .transform(new GlideCircleTransform(mContext))
        .into(imageView);
  }

  // 加载圆型头像
  public void loadCircleHead(String url, ImageView imageView) {
    url = getUrl(url);
    if (StringUtils.isBlank(url)) {
      imageView.setImageResource(R.mipmap.library_head);
    } else {
      Glide.with(mContext)
          .load(url)
          .placeholder(R.mipmap.default_image)
          .error(R.mipmap.default_image)
          .crossFade()
          .transform(new GlideCircleTransform(mContext))
          .into(imageView);
    }
  }

  // 加载圆型头像
  public void loadCircleHead(String url2name, AvatarImageView imageView) {
    url2name = getUrl(url2name);
    if (StringUtils.isBlank(url2name)) {
      imageView.setImageResource(R.mipmap.library_head);
    } else if (url2name.contains("http://") || url2name.contains("https://")) {
      Glide.with(mContext)
          .load(url2name)
          .placeholder(R.mipmap.default_image)
          .error(R.mipmap.default_image)
          .crossFade()
          .transform(new GlideCircleTransform(mContext))
          .into(imageView);
    } else {
      imageView.setTextColor(0xFF2F2F2F);
      imageView.setTextAndColor(url2name.substring(0, 1), 0xFFFFD100);
    }
  }

  // 加载圆角图片
  public void loadRoundImage(String url, ImageView imageView, float roundPx) {
    url = getUrl(url);
    Glide.with(mContext)
        .load(url)
        .error(R.mipmap.default_image)
        .crossFade()
        .bitmapTransform(new GlideRoundTransform(mContext, roundPx))
        .into(imageView);
  }

  // 加载圆角图片带边框
  public void loadRoundImage(String url, ImageView imageView, float roundPx, int bordercolor,
      int borderwidth) {
    url = getUrl(url);
    Glide.with(mContext)
        .load(url)
        .error(R.mipmap.default_image)
        .crossFade()
        .bitmapTransform(new GlideRoundTransform(mContext, roundPx, bordercolor, borderwidth))
        .into(imageView);
  }
}
